package com.lzm.base;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.transition.Fade;
import android.util.Pair;
import android.view.View;
import android.view.Window;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import com.google.android.material.transition.platform.MaterialContainerTransform;
import com.google.android.material.transition.platform.MaterialContainerTransformSharedElementCallback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

public abstract class BaseActivity<VB extends ViewDataBinding>  extends Activity {

    protected VB mBind;

    private Map<Integer,Pair<View,String>> pairMap;

    private static final String ShareElement = "ShareElements";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        pairMap = new WeakHashMap<>();
        getWindow().setEnterTransition(new Fade());
        getWindow().setExitTransition(new Fade());
        getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        setExitSharedElementCallback(new MaterialContainerTransformSharedElementCallback());
        getWindow().setSharedElementsUseOverlay(false);
        super.onCreate(savedInstanceState);
        mBind = DataBindingUtil.setContentView(this,getLayoutId());

        getIntent().getIntegerArrayListExtra(ShareElement);
        getWindow().setSharedElementEnterTransition(new MaterialContainerTransform());
        getWindow().setSharedElementEnterTransition(new MaterialContainerTransform().setDuration(1500L));
    }

    protected abstract @LayoutRes int getLayoutId();

    protected void addShareElementPair(View view, @IdRes int resId){
        pairMap.put(resId,new Pair<>(view,ShareElement + resId));
    }
    protected void startActivity(Context context, Class<?> cls){
        Intent intent = new Intent(context,cls);
        List<Pair<View,String>>
        startActivity(intent,
                ActivityOptions.makeSceneTransitionAnimation(context, pairMap.values().toArray()).toBundle());
    }
}
